﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.ServiceModel;
using TCKQKL.Service.DataContract;
using TCKQKL.Service.Config;
using TCKQKL.Service.Contract;
using TCKQKL.DAL.IDAL;
using TCKQKL.DAL.DataFactory;
using System.Net.Security;
using System.Transactions;

namespace TCKQKL.Service.Implementation
{
    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Multiple, InstanceContextMode = InstanceContextMode.PerCall)]
    public class LookupService : ILookupService
    {
        private IInvestor dalInvestor;
        private IOrdersBosc dalOrder;
        private IMarketSummary dalMarket;
        private IOrdersOnline dalOrdersOnline;

        public void PrintMessage(string message)
        {
            PrintSecurityInformation();
            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Message received [{0}]", message);
            Console.ResetColor();
        }

        private void PrintSecurityInformation()
        {
            Console.WriteLine(ServiceSecurityContext.Current.ToString());
            Console.WriteLine(ServiceSecurityContext.Current.PrimaryIdentity.ToString());
            Console.WriteLine(ServiceSecurityContext.Current.PrimaryIdentity.Name.ToString());
        }

        public DateTime getTradingDate()
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getTradingDate();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public InvestorDataModel getAccountData(string accountNo)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getInvestorData(accountNo);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<InvestorDataModel> getAssignInvestorData(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getAssignInvestorData(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<InvestorDataModel> getAssignInvestorDataByName(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getAssignInvestorDataByName(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public InvestorDataProfileModel getAccountProfileData(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getInvestorProfileData(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public InvestorDataModel loginProcess(string accountNo, string password)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.login(accountNo, password);
            }
            catch
            {
                return null;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public void logout(string accountNo)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            try
            {
                dalInvestor.logOutUser(accountNo);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<TransDataModel> getTransByInvestorId(decimal investorID, DateTime fromDate, DateTime toDate)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getTransByInvestorId(investorID, fromDate, toDate);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public decimal getRemain(decimal investorID, DateTime selected)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getRemain(investorID, selected);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrders(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrders(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersWaiting(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrdersWaiting(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersTopN(decimal investorID, int topN)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrdersTopN(investorID, topN);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public List<OrdersBoscModel> getOrdersHistory(decimal investorID, DateTime start, DateTime end)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrdersHistory(investorID, start, end);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersHistoryDetail(decimal investorID, DateTime fromDate, DateTime toDate, string orderType, string orderClass, string orderSide, string symbol, string ID, decimal priceLow, decimal priceHigh, decimal qttyLow, decimal qttyHigh)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getOrdersHistoryDetail(investorID, fromDate, toDate, orderType, orderClass, orderSide, symbol, ID, priceLow, priceHigh, qttyLow, qttyHigh);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<AccountDataModel> getAccountByInvestorID(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getAccountDataByInvestorId(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<BankDataModel> getBankByInvestorID(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getBankDataByInvestorId(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public BankDataModel getBankDataByDate(decimal investorID, DateTime selected)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getBankDataByDate(investorID, selected);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public decimal getNettingAmountByInvestorId(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getNettingAmountByInvestorId(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResult(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getMatchedResult(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResultHistory(decimal investorID, DateTime start, DateTime end)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getMatchedResultHistory(investorID, start, end);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersRelatedByOrderId(string originOrderId)
        {
            dalOrder = TCKQKL.DAL.DataFactory.OrdersBoscFactory.Create(Settings.DAL);
            dalOrder.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalOrder.getOrdersRelatedByOrderId(originOrderId);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrder.Close();
            }
        }

        public List<OrdersBoscModel> getOrdersRelatedHistoryByOrderId(string originOrderId)
        {
            dalOrder = TCKQKL.DAL.DataFactory.OrdersBoscFactory.Create(Settings.DAL);
            dalOrder.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalOrder.getOrdersRelatedHistoryByOrderId(originOrderId);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrder.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResultByOrderID(string orderID)
        {
            dalOrder = TCKQKL.DAL.DataFactory.OrdersBoscFactory.Create(Settings.DAL);
            dalOrder.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalOrder.getMatchedResultByOrderId(orderID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrder.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResultTopN(decimal investorID, int topN)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getMatchedResultTopN(investorID, topN);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public List<MatchedResultModel> getMatchedResultEarlyByAccountNo(string orderID)
        {
            dalOrder = TCKQKL.DAL.DataFactory.OrdersBoscFactory.Create(Settings.DAL);
            dalOrder.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalOrder.getMatchedResultEarlyByAccountNo(orderID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrder.Close();
            }
        }

        public List<MatchedResultModel> getMatchedResultByInvestorId(decimal investorID)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getMatchedResultByInvestorId(investorID);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public MarketSummaryDataModel getMarketSummary(string marketName)
        {
            dalMarket = TCKQKL.DAL.DataFactory.MarketSummary.Create(Settings.SQLDAL);
            dalMarket.Open(Settings.CONFIGDB_SQL_CONN_STRING);
            try
            {
                if (marketName.Equals("HOSE"))
                    return dalMarket.getMarketSummaryHose();
                else
                    return dalMarket.getMarketSummaryHastc();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalMarket.Close();
            }
        }

        public StocksDataModel getQuote(string symbol)
        {
            dalMarket = TCKQKL.DAL.DataFactory.MarketSummary.Create(Settings.SQLDAL);
            dalMarket.Open(Settings.CONFIGDB_SQL_CONN_STRING);
            try
            {
                StocksDataModel hoStock = dalMarket.getQuoteHose(symbol);
                if (hoStock != null)
                    return hoStock;
                else
                    return dalMarket.getQuoteHastc(symbol);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalMarket.Close();
            }
        }

        public int PlaceOrder(OrdersOnlineModel order)
        {
            DataAccessClient dac = new DataAccessClient();
            try
            {
                StocksDataModel stock = dac.getStockInfoBySymbol(order.Symbol);
                OrdersOnlineModel recentOrder = dac.GetMostRecentOrder(order.AccountNo, order.OrderSide, order.OpenDate, order.ExpireDate, order.Symbol, order.Price, order.Quantity, "CANCEL");

                #region Order placement
                switch (order.OrderSide)
                {
                    case "BUY":
                        List<OrdersOnlineModel> list = dac.getOnlineOrdersDetail(order.AccountNo, "SELL", order.Symbol, null, null, null, order.TimeStamp, null, null, null, 0);

                        //Check weather they have enough money to buy
                        if (dac.IsEnoughMoney(order))
                            if (list.Count == 0)
                                dac.InsertNewOrder(order);
                            else
                                return 61;
                        else
                            return 51;
                        break;
                    case "SELL":
                        List<OrdersOnlineModel> list2 = dac.getOnlineOrdersDetail(order.AccountNo, "BUY", order.Symbol, null, null, null, order.TimeStamp, null, null, null, 0);


                        //Check weather they have enough money to buy
                        if (dac.IsEnoughStock(order.AccountNo, order.Symbol, order.Quantity))
                            if (list2.Count == 0)
                                dac.InsertNewOrder(order);
                            else
                                return 62;
                        else
                            return 52;
                        break;
                }
                #endregion

                return 50;
            }
            catch
            {
                return 100;
            }

        }

        public int ChangeOrder(OrdersOnlineModel original, OrdersOnlineModel changed)
        {
            DataAccessClient dac = new DataAccessClient();
            StocksDataModel stock = dac.getStockInfoBySymbol(original.Symbol);
            //Locate original order
            //OrdersOnlineModel oOrder = dac.GetMostRecentOrder(original.OrderId);
            OrdersOnlineModel rOrder = dac.GetMostRecentOrder(original.AccountNo, original.OrderSide, original.OpenDate, original.ExpireDate, original.Symbol, original.Price, original.Quantity, "CANCEL");

            #region Order change
            if (rOrder != null)
            {
                if (rOrder.OrderType.Equals("PLACE"))
                {
                    if (rOrder.OrderNo.Equals(""))
                    {
                        //Insert new change order
                        switch (original.OrderSide)
                        {
                            case "BUY":
                                //Check weather they have enough money to buy
                                if (dac.IsEnoughMoney(changed))
                                    dac.UpdateChangeOrder(rOrder.OrderLineNum, changed.Price);
                                else
                                    return 51;
                                break;
                            case "SELL":
                                //Check weather they have enough money to buy
                                if (dac.IsEnoughStock(changed.AccountNo, changed.Symbol, changed.Quantity))
                                    dac.UpdateChangeOrder(rOrder.OrderLineNum, changed.Price);
                                else
                                    return 52;
                                break;
                        }
                    }
                    else
                    {
                        if (stock.exCenter.Equals("HOSE"))
                            return 62;
                        else
                        {
                            switch (original.OrderSide)
                            {
                                case "BUY":
                                    //Check weather they have enough money to buy
                                    if (dac.IsEnoughMoney(changed))
                                        dac.InsertChangeOrder(rOrder.OrderId, "", changed.TimeStamp, changed.Quantity, changed.Price, "CHANGE");
                                    else
                                        return 51;
                                    break;
                                case "SELL":
                                    //Check weather they have enough money to buy
                                    if (dac.IsEnoughStock(original.AccountNo, changed.Symbol, changed.Quantity))
                                        dac.InsertChangeOrder(rOrder.OrderId, "", changed.TimeStamp, changed.Quantity, changed.Price, "CHANGE");
                                    else
                                        return 52;
                                    break;
                            }
                        }
                    }
                    //Insert new change order

                }
                else
                    if (rOrder.OrderType.Equals("CHANGE"))
                    {
                        if (rOrder.OrderNo.Equals(""))
                        {
                            //change unprocessed order
                            switch (original.OrderSide)
                            {
                                case "BUY":
                                    //Check weather they have enough money to buy
                                    if (dac.IsEnoughMoney(changed))
                                        dac.UpdateChangeOrder(rOrder.OrderLineNum, changed.Price);
                                    else
                                        return 51;
                                    break;
                                case "SELL":
                                    //Check weather they have enough money to buy
                                    if (dac.IsEnoughStock(original.AccountNo, changed.Symbol, changed.Quantity))
                                        dac.UpdateChangeOrder(rOrder.OrderLineNum, changed.Price);
                                    else
                                        return 52;
                                    break;
                            }

                        }
                        else
                        {
                            //Insert newly change order
                            switch (original.OrderSide)
                            {
                                case "BUY":
                                    //Check weather they have enough money to buy
                                    if (dac.IsEnoughMoney(changed))
                                        dac.InsertChangeOrder(rOrder.OrderId, "", changed.TimeStamp, changed.Quantity, changed.Price, "CHANGE");
                                    else
                                        return 51;
                                    break;
                                case "SELL":
                                    //Check weather they have enough money to buy
                                    if (dac.IsEnoughStock(original.AccountNo, changed.Symbol, changed.Quantity))
                                        dac.InsertChangeOrder(rOrder.OrderId, "", changed.TimeStamp, changed.Quantity, changed.Price, "CHANGE");
                                    else
                                        return 52;
                                    break;
                            }

                        }
                    }
                    else
                        return 61; // INVALID ORDER CHANGE
            }
            else
                return 60; //Original order not found
            #endregion

            return 50;
        }

        public int CancelOrder(OrdersOnlineModel original)
        {
            DataAccessClient dac = new DataAccessClient();
            StocksDataModel stock = dac.getStockInfoBySymbol(original.Symbol);
            //Locate original order
            OrdersOnlineModel oOrder = dac.GetMostRecentOrder(original.AccountNo, original.OrderSide, original.OpenDate, original.ExpireDate, original.Symbol, original.Price, original.Quantity, "CANCEL");

            if (oOrder != null)
            {
                if (oOrder.OrderNo.Equals(""))
                {
                    dac.DeleteOrderByOrdePlacerId(oOrder.OrderLineNum);
                    if (oOrder.OrderType.Equals("CHANGE"))
                        dac.InsertCancelOrder(oOrder.OrderId, "", DateTime.Now, original.Quantity, original.Price, "CANCEL");
                }
                else
                {
                    if (stock.exCenter.Equals("HOSE"))
                    {
                        if (original.OrderClass.Equals("ATO") || original.OrderClass.Equals("ATC"))
                            return 64;
                        else
                        {
                            int phase = getPhaseByHour(oOrder.TimeStamp);
                            int cPhase = getPhaseByHour(DateTime.Now);
                            if (cPhase > phase)
                                dac.InsertCancelOrder(oOrder.OrderId, "", DateTime.Now, original.Quantity, original.Price, "CANCEL");
                            else
                                return 65;
                        }
                    }
                    else
                        dac.InsertCancelOrder(oOrder.OrderId, "", DateTime.Now, original.Quantity, original.Price, "CANCEL");
                }

            }
            else
                return 60; //Original order not found

            return 50;
        }

        private int getPhaseByHour(DateTime stamp)
        {
            if (stamp.Hour.Equals(8) && stamp.Minute < 50)
                return 1;
            else
                if (stamp.Hour.Equals(9) || (stamp.Hour.Equals(10) && stamp.Minute < 8))
                    return 2;
                else
                    if (stamp.Hour.Equals(10) && stamp.Minute < 20)
                        return 3;
            return 0;
        }

        public StocksDataModel GetStockInfo(string symbol)
        {
            DataAccessClient dac = new DataAccessClient();
            return dac.getStockInfoBySymbol(symbol);
        }

        public List<OrdersOnlineModel> getOrderListByStatus(string Status)
        {
            dalOrdersOnline = TCKQKL.DAL.DataFactory.OrdersOnlineFactory.Create(Settings.SQLDAL);
            dalOrdersOnline.Open(Settings.CONFIGDB_SQL_ORDERS);
            try
            {
                return dalOrdersOnline.getOrderListByStatus(Status);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrdersOnline.Close();
            }
        }

        public List<OrdersOnlineModel> getOrderListByStatusAndAccountNo(string Status, string AccountNo)
        {
            dalOrdersOnline = TCKQKL.DAL.DataFactory.OrdersOnlineFactory.Create(Settings.SQLDAL);
            dalOrdersOnline.Open(Settings.CONFIGDB_SQL_ORDERS);
            try
            {
                return dalOrdersOnline.getOrderListByStatusAndAccountNo(Status, AccountNo);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrdersOnline.Close();
            }
        }

        public List<OrdersOnlineModel> getOnlineOrdersDetail(string AccountNo, string OrderSide, string Symbol, string OrderSpec, string OrderClass, string OrderMode, DateTime TimeStamp, string Price, string OrderType, string OrderNo, int Quantity)
        {
            DataAccessClient dac = new DataAccessClient();
            return dac.getOnlineOrdersDetail(AccountNo, OrderSide, Symbol, OrderSpec, OrderClass, OrderMode, TimeStamp, Price, OrderType, OrderNo, Quantity);
        }

        public OrdersOnlineModel getNewestOrder(string OrgStatus, string StatusAfter)
        {
            dalOrdersOnline = TCKQKL.DAL.DataFactory.OrdersOnlineFactory.Create(Settings.SQLDAL);
            dalOrdersOnline.Open(Settings.CONFIGDB_SQL_ORDERS);
            try
            {
                return dalOrdersOnline.getNewestOrder(OrgStatus, StatusAfter);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrdersOnline.Close();
            }
        }

        public OrdersOnlineModel GetOrderByOrderId(int OrderPlaceId)
        {
            dalOrdersOnline = TCKQKL.DAL.DataFactory.OrdersOnlineFactory.Create(Settings.SQLDAL);
            dalOrdersOnline.Open(Settings.CONFIGDB_SQL_ORDERS);
            try
            {
                return dalOrdersOnline.GetOrderByOrderId(OrderPlaceId);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrdersOnline.Close();
            }
        }

        public int UpdateOrderStatus(int OrderPlaceId, string Status)
        {
            dalOrdersOnline = TCKQKL.DAL.DataFactory.OrdersOnlineFactory.Create(Settings.SQLDAL);
            dalOrdersOnline.Open(Settings.CONFIGDB_SQL_ORDERS);
            try
            {
                return dalOrdersOnline.UpdateOrderStatus(OrderPlaceId, Status);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalOrdersOnline.Close();
            }
        }

        public OrdersOnlineModel getOrderOriginal(int OrderId)
        {
            DataAccessClient client = new DataAccessClient();
            return client.getOrderOriginal(OrderId);
        }

        public List<DateTime> getBackDate()
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getBackDate();
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }
        }

        public decimal getPreLoanByInvestor(decimal investorid)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getPreLoanByInvestor(investorid);
            }
            catch
            {
                throw;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

        public List<AccountDataModel> getStockNettingInvestorId(decimal investorID, DateTime transdate)
        {
            dalInvestor = TCKQKL.DAL.DataFactory.InvestorFactory.Create(Settings.DAL);
            dalInvestor.Open(Settings.CONFIGDB_ORACLE_CONN_STRING);
            try
            {
                return dalInvestor.getStockNettingInvestorId(investorID, transdate);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                dalInvestor.Close();
            }

        }

    }
}
